import { AudioClass } from "./audio";
import firAudio from "./../assets/fir-audio.mp3";
import jinggao from "./../assets/jinggao.mp3";

const FIR_STATUS_AWAIT = Symbol("停止");
const FIR_STATUS_FIR = Symbol("任务执行中");
const FIR_STATUS_DONE = Symbol("任务执行完成");
const FIR_STATUS_DESTORY = Symbol("销毁");

export class Fir {
  name = "";
  setName(name) {
    this.name = name;
  }

  image = new Image();
  setImage(src) {
    this.image.src = src;
  }

  width = 0;
  setWidth(value) {
    this.width = value;
  }

  height = 0;
  setHeight(value) {
    this.height = value;
  }

  maxSpeed = 0;
  setMaxSpeed(speed) {
    this.maxSpeed = speed;
  }

  minPosition = [0, 0];
  setMinPosition(x, y) {
    this.minPosition = [x, y];
  }

  maxPosition = [0, 0];
  setMaxPosition(x, y) {
    this.maxPosition = [x, y];
  }

  position = [0, 0];
  setPosition(x, y) {
    this.position = [x, y];
  }

  color = "red";
  setColor(color) {
    this.color = color;
  }

  status = FIR_STATUS_AWAIT;
  setStatus(status) {
    this.status = status;
  }

  commond(action) {
    try {
      this[action]();
    } catch (e) {
      console.error("指令机动异常", e);
    }
  }

  onDestoryCallback = []
  destory() {
    this.setStatus(FIR_STATUS_DESTORY);
    this.closeFirVolce();
    this.closeWarnVolce();
    this.setPosition(innerWidth + 100, -100);
    this.onDestoryCallback?.forEach(fn => fn())
  }
  ondestory(callback){
    this.onDestoryCallback.push(callback);
  }
  removeDestory(callback){
    const index = this.onDestoryCallback?.findIndex(item => item === callback)
    if (index > -1){
      this.onDestoryCallback.splice(index, 1);
    }
  }

  firVolce = null;
  openFirVolce() {
    if (!this.firVolce) {
      this.firVolce = new AudioClass(firAudio).volume(0.5);
    }
  }
  closeFirVolce() {
    if (this.firVolce) {
      this.firVolce.destory?.();
    }
    this.firVolce = null;
  }

  warnVolce = null;
  openWarnVolce() {
    if (!this.warnVolce) {
      this.warnVolce = new AudioClass(jinggao).loop();
    }
  }
  closeWarnVolce() {
    if (this.warnVolce) {
      this.warnVolce.destory?.();
    }
    this.warnVolce = null;
  }
}
